package com.blr.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.sql.DataSource;
import java.util.UUID;

/**
 * ClassName: SecurityConfig
 * Description:
 * Date: 2022/11/07 PM 2:27
 * <p>
 * project: codes
 * package: com.blr.config
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final DataSource dataSource;

    @Autowired
    public SecurityConfig(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Bean
    public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
        inMemoryUserDetailsManager.createUser(User.withUsername("root").password("{noop}123").roles(
                "admin").build());
        return inMemoryUserDetailsManager;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService());
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                // 指定资源记住我
                //.mvcMatchers("/index").rememberMe()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .and()
                // 开启记住我配置
                .rememberMe()
                .tokenRepository(persistentTokenRepository())
                // 指定 rememberService 实现
                //.rememberMeServices(rememberMeServices())
                // 用来接收请求中哪个参数作为开启记住我的参数
                //.rememberMeParameter("remember-me")
                // 总是记住我
                //.alwaysRemember(true)
                .and()
                .csrf().disable();
    }

    // 指定数据库持久化
    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        // 启动创建表结构
        jdbcTokenRepository.setCreateTableOnStartup(false);
        return jdbcTokenRepository;
    }

    ///**
    // * 指定记住我的实现
    // *
    // * @return rememberMeServices
    // */
    //@Bean
    //public RememberMeServices rememberMeServices() {
    //    JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
    //    tokenRepository.setDataSource(dataSource);
    //    // 启动时创建表结构
    //    //tokenRepository.setCreateTableOnStartup(true);
    //    return new PersistentTokenBasedRememberMeServices(UUID.randomUUID().toString(),
    //            userDetailsService(), tokenRepository);
    //}
}
